var dragging =
{
	state: null,
	
	init: function()
	{
		var trash = $("#trashCan");
		if (trash.length === 0)
		{
			trash = $("body").append("<div id=\"trashCan\"><div class=\"item\"></div></div>");
		}
		return trash;
	},
	
	start: function(e)
	{
		var newState = { dropEffect: "" };
		newState.item = $(this);
		newState.itemIndex = newState.item.prevAll(".item").length;
		newState.itemData = newState.item.data("object");
		newState.srcContainer = newState.item.closest(".container");
		newState.srcContainerData = newState.srcContainer.data("object");
		newState.readOnly = newState.srcContainer.is(".readOnly");
		newState.allowed = newState.readOnly ? "copy" : "move";
		
		dragging.state = newState;
		e.originalEvent.dataTransfer.effectAllowed = newState.allowed; //copy, move, copyMove
		e.originalEvent.dataTransfer.setData("text/plain", JSON.stringify(newState.itemData));
		
		$(this).addClass("dragging");
		
		if (!dragging.state.readOnly)
		{
			var trash = dragging.init();
			trash.css({left: "-71px", top: "0"});
			trash.offset($(this).closest(".container").offset());
			trash.fadeIn();
		}
	},
	
	drop: function(e)
	{
		var result = dragging._stop.call(this, e, true);
		
		var targetIndex = -1;
		if (result.targetItem)
			targetIndex = result.targetItem.prevAll(".item").length;
		if (result.dropEffect === "move")
		{
			if (result.srcContainerData.id === result.targetContainerData.id && targetIndex > result.itemIndex)
				targetIndex--;
			result.srcContainerData.remove(result.itemData);
		}
		result.targetContainerData.add(result.itemData, targetIndex);

		options.setContainer(result.targetContainerData.id, function()
		{
			this.items = result.targetContainerData.items.data();
		});
	},
	
	cancel: function(e)
	{
		dragging._stop.call(this, e, false);
	},
	
	over: function(e)
	{
		e.preventDefault();
		if ($(this).is(".readOnly"))
		{
			e.originalEvent.dataTransfer.dropEffect = "none";
		}
		else if ($(this).is("#trashCan"))
		{
			e.originalEvent.dataTransfer.dropEffect = "move";
		}
		else
		{
			if (e.originalEvent.dataTransfer.getData("text/plain"))
				e.originalEvent.dataTransfer.dropEffect = e.originalEvent.dataTransfer.effectAllowed;
		}
	},
	
	enter: function(e)
	{
		var targetContainer = $(this).closest(".container");
		var targetItem = $(e.target);
		if (targetItem.is(".container"))
			targetItem = null;
		else
			targetItem = targetItem.closest("a.item");
		
		e.preventDefault();
		if (!$(this).is(".readOnly"))
		{
			if (targetItem && targetItem.length > 0)
				targetItem.addClass("droppingOnto");
			$(".droppingOnto").not(targetItem).removeClass("droppingOnto");
		}
	},
	
	trash: function(e)
	{
		var result = dragging._stop.call(this, e, true);
		result.dropEffect = "move";
		
		result.srcContainerData.remove(result.itemData);
	
		options.setContainer(result.srcContainerData.id, function()
		{
			this.items = result.srcContainerData.items.data();
		});
	},
	
	_stop: function(e, isDropping)
	{
		var result = null;
		e.preventDefault();
		
		if (isDropping)
		{
			result = dragging.state;
			result.dropEffect = result.allowed;
			result.dropTarget = $(this);
			result.targetContainer = result.dropTarget.closest(".container");
			result.targetContainerData = result.targetContainer.data("object");
			
			result.targetItem = $(".droppingOnto");
			if (result.targetItem.length > 0)
			{
				result.targetItemData = result.targetItem.data("object");
			}
			else
			{
				result.targetItem = null;
				result.targetItemData = null;
			}
		}
		
		$(".dragging").removeClass("dragging");
		$(".droppingOnto").removeClass("droppingOnto");
		$("#trashCan").fadeOut("fast");
		
		dragging.state = null;
		return result;
	},
	
	_prevent: function(e)
	{
		e.preventDefault();
	}
};

$(".designer .container")
	.live("drop", dragging.drop)
	.live("dragover", dragging.over)
	.live("dragenter", dragging.enter);

/*
function(e)
{
	
	var field = $(this);
	var targetContainer = field.closest(".ui-tabs-panel").find(".container");
	var targetContainerObj = targetContainer.data("object");
	
	var domId = field.closest(".ui-tabs-panel").attr("id");
	var id = targetContainerObj.data("id");
	
	var srcContainer = $(dragging.contextContainer);
	var srcContainerObj = srcContainer.data("object");
	
	var srcData = e.originalEvent.dataTransfer.getData("text/plain");
	var src = JSON.parse(srcData);
	
	dragging.dropEffect = e.originalEvent.dataTransfer.effectAllowed;
	
	if (dragging.dropEffect === "move")
	{
		srcContainerObj.remove(src);
	}

	var targetItem = $(".droppingOnto");
	if (targetItem.length > 0)
	{
		Array.prototype.splice.call(targetContainerObj.items, targetItem.prevAll(".item").length, 0, src);
	}
	else
	{
		targetContainerObj.add([JSON.parse(src)]);
	}
	
	options.setContainer(id, function()
	{
		this.items = targetContainerObj.items.data();
	});
	
})
.live("dragover", function(e)
{
	e.preventDefault();
	if ($(this).is(".readOnly"))
	{
		e.originalEvent.dataTransfer.dropEffect = "none";
	}
	else
	{
		if (e.originalEvent.dataTransfer.getData("text/plain"))
			e.originalEvent.dataTransfer.dropEffect = e.originalEvent.dataTransfer.effectAllowed;
	}
})
.live("dragenter", function(e)
{
	var targetContainer = $(this).closest(".container");
	var targetItem = $(e.target);
	if (targetItem.is(".container"))
		targetItem = null;
	else
		targetItem = targetItem.closest("a.item");
	
	e.preventDefault();
	if (!$(this).is(".readOnly"))
	{
		if (targetItem && targetItem.length > 0)
			targetItem.addClass("droppingOnto");
		$(".droppingOnto").not(targetItem).removeClass("droppingOnto");
	}
})
;
*/

$("#trashCan")
	.live("drop", dragging.trash)
	.live("dragover", dragging.over)
	.live("dragenter", dragging.enter);

$(".designer .container a.item")
	.attr("draggable", true)
	.live("dragstart", dragging.start)
	.live("click", dragging._prevent);



$(window).load(dragging.init);
